package com.gosophia.accountManagement.action;

import java.util.List;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.userManagement.dto.PermissionDTO;
import com.gosophia.userManagement.dto.RoleDTO;
import com.gosophia.userManagement.dto.UserAccountDTO;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.gosophia.userManagement.ws.UserManagementWebService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 跳转到编辑页面
 * @author mayue
 *
 */
@Component("ToEditPageAction")
@Scope("prototype")
public class ToEditPageAction extends BaseSupportAction {
    
    private static final long serialVersionUID = 1L;

    @Autowired
    private UserManagementWebService userManagementWebService;

    private UserAccountDTO userAccountDTO;
    
    private Long userAccountId;
    
    //账号使用者列表(List<UserInfoDTO>)
    private String accountUsersJsonString;
    //可使用的账号角色
    private List<RoleDTO> canUserRoles;
    //账号权限(List<PermissionDTO>)
    private String accountPermissionJsonString;
    
    private String errorMsgs;
    
    @Override
    public String _execute() throws Exception {
        userAccountDTO = userManagementWebService.findUserAcountByUserAcountIdEdit(userAccountId);
        if(userAccountDTO!=null){
            canUserRoles = userManagementWebService.findAllRole();
            List<RoleDTO> userHasRoles = userAccountDTO.getRoleDTOs();
            if(userHasRoles!=null && userHasRoles.size()!=0){
                for(RoleDTO roleDTO : userHasRoles){
                    if(canUserRoles.contains(roleDTO)){
                        canUserRoles.remove(roleDTO);
                    }
                }
            }
            List<UserInfoDTO> userInfos = userManagementWebService.findAllUserInfo();
            if(userInfos!=null){
                accountUsersJsonString = JSONArray.fromObject(userInfos).toString().replace("\"", "\\\"");
            }
            List<PermissionDTO> userPermissionDtos= userAccountDTO.getPermissionDTOs();
            if(userPermissionDtos!=null){
                accountPermissionJsonString = JSONArray.fromObject(userPermissionDtos).toString().replace("\"", "\\\"");
            }
        }else{
            BusinessException exception = new BusinessException("该账号已被删除,请刷新页面后重新操作", "JC2_OA_UA_02");
            super.getRequestResult().setRsltCode(exception.getFaultInfo().getErrorCode());
            super.getRequestResult().setMessage(exception.getFaultInfo().getMessage());
            ActionContext.getContext().getSession().put("_lastOpResult", super.getRequestResult()); 
            throw exception;
        }
        return SUCCESS;
    }
    public UserAccountDTO getUserAccountDTO() {
        return userAccountDTO;
    }
    public void setUserAccountDTO(UserAccountDTO userAccountDTO) {
        this.userAccountDTO = userAccountDTO;
    }
    public List<RoleDTO> getCanUserRoles() {
        return canUserRoles;
    }
    public void setCanUserRoles(List<RoleDTO> canUserRoles) {
        this.canUserRoles = canUserRoles;
    }
    public String getAccountPermissionJsonString() {
        return accountPermissionJsonString;
    }
    public void setAccountPermissionJsonString(String accountPermissionJsonString) {
        this.accountPermissionJsonString = accountPermissionJsonString;
    }
    public Long getUserAccountId() {
        return userAccountId;
    }
    public void setUserAccountId(Long userAccountId) {
        this.userAccountId = userAccountId;
    }
    public String getAccountUsersJsonString() {
        return accountUsersJsonString;
    }
    public void setAccountUsersJsonString(String accountUsersJsonString) {
        this.accountUsersJsonString = accountUsersJsonString;
    }
    public String getErrorMsgs() {
        return errorMsgs;
    }
    public void setErrorMsgs(String errorMsgs) {
        this.errorMsgs = errorMsgs;
    }
    
    
    
}
